<?php
function seitcm_init_database()
{
	global $wpdb;
	$wpdb->query('CREATE TABLE IF NOT EXISTS ' . SEITCM_TABLE_COURSES . '(
					id INT(11) NOT NULL AUTO_INCREMENT,
					codename VARCHAR(10) NOT NULL,
					name VARCHAR(200) NOT NULL,
					note TEXT,
					PRIMARY KEY (`id`)
					) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
					');
	$wpdb->query('CREATE TABLE IF NOT EXISTS ' . SEITCM_TABLE_CURRICULUM . '(
					id INT(11) NOT NULL AUTO_INCREMENT,
					coursesid INT(11) NOT NULL,
					title VARCHAR(100) NOT NULL,
					sumary TEXT,
					`order` INT(3) NOT NULL,
					PRIMARY KEY (`id`),
					FOREIGN KEY (coursesid) REFERENCES '.$wpdb->prefix.'seitcm_courses(id)
					) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
					');
	$wpdb->query('CREATE TABLE IF NOT EXISTS ' . SEITCM_TABLE_FEES . '(
					id INT(11) NOT NULL AUTO_INCREMENT,
					coursesid INT(11) NOT NULL,
					cost DECIMAL(13.2) NOT NULL,
					applysince DATETIME NOT NULL,
					PRIMARY KEY (`id`),
					FOREIGN KEY (coursesid) REFERENCES '.$wpdb->prefix.'seitcm_courses(id)
					) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
					');
	$wpdb->query('CREATE TABLE IF NOT EXISTS ' . SEITCM_TABLE_CLASS . '(
					id INT(11) NOT NULL AUTO_INCREMENT,
					classcode VARCHAR(50) NOT NULL,
					coursesid INT(11) NOT NULL,
					teacherid BIGINT(20) NOT NULL,
					startdate DATETIME NOT NULL,
					alowreg bit NOT NULL DEFAULT 0,
					finish bit NOT NULL DEFAULT 0,
					learntime INT NOT NULL,
					note TEXT,
					PRIMARY KEY (`id`),
					FOREIGN KEY (coursesid) REFERENCES '.$wpdb->prefix.'seitcm_courses(id)
					) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
					');
	$wpdb->query('CREATE TABLE IF NOT EXISTS ' . SEITCM_TABLE_SCHEDULE . '(
					id INT(11) NOT NULL AUTO_INCREMENT,
					classid INT(11) NOT NULL,
					title VARCHAR(100) NOT NULL,
					curriculumid INT(11) NOT NULL,
					note TEXT,
					time DATETIME NOT NULL,
					place VARCHAR(200),
					`status` BIT NOT NULL DEFAULT 1,
					PRIMARY KEY (`id`),
					FOREIGN KEY (classid) REFERENCES ' . SEITCM_TABLE_CLASS . '(id)
					) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
					');
	$wpdb->query('CREATE TABLE IF NOT EXISTS ' . SEITCM_TABLE_REGISTER . '(
					id INT(11) NOT NULL AUTO_INCREMENT,
					studentid INT(11) NOT NULL,
					classid INT(11) NOT NULL,
					discounts INT NOT NULL DEFAULT 0,
					collectedfee DECIMAL(13.2) NOT NULL DEFAULT 0,
					time DATETIME NOT NULL,
					note TEXT,
					PRIMARY KEY (`id`),
					FOREIGN KEY (`classid`) REFERENCES '. SEITCM_TABLE_CLASS .'(id)
					) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
					');
	$wpdb->query('CREATE TABLE IF NOT EXISTS ' . SEITCM_TABLE_ROLLUP . '(
					id INT(11) NOT NULL AUTO_INCREMENT,
					studentid INT(11) NOT NULL,
					scheduleid INT(11) NOT NULL,
					note TEXT,
					checkin BIT NOT NULL DEFAULT 0,
					PRIMARY KEY (`id`),
					FOREIGN KEY (`scheduleid`) REFERENCES '. SEITCM_TABLE_SCHEDULE .'(id)
					) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
					');
	$wpdb->query('CREATE TABLE IF NOT EXISTS ' . SEITCM_TABLE_SCORE . '(
					id INT(11) NOT NULL AUTO_INCREMENT,
					studentid INT(11) NOT NULL,
					classid INT(11) NOT NULL,
					points DECIMAL(5,2) NOT NULL,
					skill INT NOT NULL,
					type bit NOT NULL,
					time DATETIME NOT NULL,
					PRIMARY KEY (`id`),
					FOREIGN KEY (`classid`) REFERENCES '. SEITCM_TABLE_CLASS .'(id)
					) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
					');
}
function seitcm_destroy_database() {
	global $wpdb;
	$wpdb->query('DROP TABLE IF EXISTS ' . SEITCM_TABLE_SCORE);
	$wpdb->query('DROP TABLE IF EXISTS ' . SEITCM_TABLE_ROLLUP);
	$wpdb->query('DROP TABLE IF EXISTS ' . SEITCM_TABLE_REGISTER);
	$wpdb->query('DROP TABLE IF EXISTS ' . SEITCM_TABLE_SCHEDULE);
	$wpdb->query('DROP TABLE IF EXISTS ' . SEITCM_TABLE_SCORES);
	$wpdb->query('DROP TABLE IF EXISTS ' . SEITCM_TABLE_CLASS);
	$wpdb->query('DROP TABLE IF EXISTS ' . SEITCM_TABLE_FEES);
	$wpdb->query('DROP TABLE IF EXISTS ' . SEITCM_TABLE_CURRICULUM);
	$wpdb->query('DROP TABLE IF EXISTS ' . SEITCM_TABLE_COURSES);
}
?>